package com.zhuiyun.educations.api.sysmenuback.service.impl;

import com.zhuiyun.educations.api.sysmenuback.entiy.SysMenuBack;
import com.zhuiyun.educations.api.sysmenuback.mapper.SysMenuBackMapper;
import com.zhuiyun.educations.api.sysmenuback.service.SysMenuBackService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName SysMenuBackServiceImpl
 * @Description TODO
 * @Author MJL
 * @Date 2023/6/19 9:28
 **/
@Service
public class SysMenuBackServiceImpl implements SysMenuBackService {
    @Resource
    SysMenuBackMapper sysMenuBackMapper;

    /**
    * @Author MJL
    * @Description todo 菜单表全查（左侧导航栏）
    * @Date 2023/6/19 10:03
    * @Return  * @param null
    */
    @Override
    public List<SysMenuBack> SysMenuList(String userLoginName) {
        List<SysMenuBack> sysMenuBacks = sysMenuBackMapper.SysMenuList(userLoginName);
        List<SysMenuBack> result = new ArrayList<>();
        for (SysMenuBack commentEntity:sysMenuBacks){
            if (commentEntity.getMenuParentId()==0){
                result.add(getChild(commentEntity,sysMenuBacks));
            }
        }
        return result;
    }

    /**
    * @Author MJL
    * @Description todo 菜单表全查
    * @Date 2023/6/20 18:10
    * @Return  * @param null
    */
    @Override
    public List<SysMenuBack> SysMenuBackList(SysMenuBack sysMenuBack) {
        List<SysMenuBack> sysMenuBacks = sysMenuBackMapper.SysMenuBackList(sysMenuBack);
        List<SysMenuBack> result = new ArrayList<>();
        for (SysMenuBack commentEntity:sysMenuBacks){
            if (commentEntity.getMenuParentId() == 0){
                result.add(getChild(commentEntity,sysMenuBacks));
            }
        }
        return result;
    }

    /**
    * @Author MJL
    * @Description todo 菜单表添加目录
    * @Date 2023/6/20 18:11
    * @Return  * @param null
    */
    @Override
    public int SysMenuDirectoryAdd(SysMenuBack sysMenuBack) {
        Date date = new Date();
        sysMenuBack.setMenuType(1);
        sysMenuBack.setMenuParentId(0);
        sysMenuBack.setMenuSelectLogotype("menuSelect");
        sysMenuBack.setCreateTime(date);
        sysMenuBack.setDelFlag(0);
        return sysMenuBackMapper.SysMenuDirectoryAdd(sysMenuBack);
    }

    /**
    * @Author MJL
    * @Description todo 菜单表添加子菜单
    * @Date 2023/6/20 19:06
    * @Return  * @param null
    */
    @Override
    public int SysMenuSunAdd(SysMenuBack sysMenuBack) {
        Date date = new Date();
        sysMenuBack.setMenuType(2);
        sysMenuBack.setCreateTime(date);
        sysMenuBack.setDelFlag(0);
        return sysMenuBackMapper.SysMenuSunAdd(sysMenuBack);
    }

    /**
    * @Author MJL
    * @Description todo 获取下拉菜单数据（父级名称）
    * @Date 2023/6/20 20:53
    * @Return  * @param null
    */
    @Override
    public List<SysMenuBack> MenuSelectList(String menuSelectLogotype) {
        return sysMenuBackMapper.MenuSelectList(menuSelectLogotype);
    }

    /**
    * @Author MJL
    * @Description todo 菜单表删除
    * @Date 2023/6/21 11:34
    * @Return  * @param null
    */
    @Override
    public int SysMenuUpdateDelete(Integer id) {
        return sysMenuBackMapper.SysMenuUpdateDelete(id);
    }

    /**
    * @Author MJL
    * @Description todo 菜单表修改目录
    * @Date 2023/6/21 13:11
    * @Return  * @param null
    */
    @Override
    public int SysMenuDirectoryUpdate(SysMenuBack sysMenuBack) {
        Date date = new Date();
        sysMenuBack.setUpdateTime(date);
        return sysMenuBackMapper.SysMenuDirectoryUpdate(sysMenuBack);
    }

    /**
    * @Author MJL
    * @Description todo 菜单表修改子菜单
    * @Date 2023/6/21 14:42
    * @Return  * @param null
    */
    @Override
    public int SysMenuSunUpdate(SysMenuBack sysMenuBack) {
        Date date = new Date();
        sysMenuBack.setUpdateTime(date);
        return sysMenuBackMapper.SysMenuSunUpdate(sysMenuBack);
    }

    /**
    * @Author MJL
    * @Description todo 递归
    * @Date 2023/6/20 14:46
    * @Return  * @param null
    */
    private SysMenuBack getChild(SysMenuBack commentEntity, List<SysMenuBack> commentEntityList) {
        List<SysMenuBack> newCommentEntity = new ArrayList<>();
        for(SysMenuBack childComment:commentEntityList){
            if(commentEntity.getId().equals(childComment.getMenuParentId())){
                newCommentEntity.add(getChild(childComment,commentEntityList));
            }
        }
        commentEntity.setChildren(newCommentEntity);
        return commentEntity;
    }
}
